package com.suineg.pms.commons.wabacus;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suineg.pms.commons.AppConfig;
import com.suineg.pms.entity.base.Room;
import com.suineg.pms.service.base.RoomService;
import com.wabacus.config.component.application.report.ReportBean;
import com.wabacus.system.ReportRequest;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditActionBean;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wabacus.system.intercept.AbsInterceptorDefaultAdapter;
import com.wabacus.util.Consts;

/**
 *
 *@author Suine
 */
public class HouseIntercepter extends AbsInterceptorDefaultAdapter{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public int doSavePerAction(ReportRequest rrequest, ReportBean rbean, Map<String, String> mRowData, Map<String, String> mParamValues, AbsEditActionBean actionbean,
			AbsEditableReportEditDataBean editbean) {
		// TODO Auto-generated method stub
		checkForm(rrequest,mRowData);
		return super.doSavePerAction(rrequest, rbean, mRowData, mParamValues, actionbean, editbean);
	}
	
	/**
	 * 检查数据合法性
	 */
	public void checkForm(ReportRequest rrequest,Map<String, String> mRowData){
		String id = mRowData.get("id");
		String buildingId = mRowData.get("building_id");
		String unit = mRowData.get("unit");
		String roomNo = mRowData.get("room_no");
		String houseCode = mRowData.get("house_no");
		
		List<Room> list = null;
		Room room = null;
		RoomService service = AppConfig.getInstance().getWac().getBean(RoomService.class);
		
		Long bid = Long.valueOf(buildingId);
		
		if(id == null || id.isEmpty()){			//插入
			room = service.findByHouseCode(houseCode);
			if(room != null){
				rrequest.getWResponse().getMessageCollector().warn("此房屋编号已存在！", false, Consts.STATECODE_FAILED);
			}
			
			list = service.findByBuildingAndUnitAndRoomNo(bid, unit, roomNo);
			if(list != null && !list.isEmpty()){
				rrequest.getWResponse().getMessageCollector().warn("此室号已存在！", false, Consts.STATECODE_FAILED);
			}
		}else{			//修改
			room = service.get(Long.valueOf(id));
			if(room == null){		//这是非法操作
				rrequest.getWResponse().getMessageCollector().warn("输入的数据有误，请重新登陆！", false, Consts.STATECODE_FAILED);
			}
			
			if(!room.getHouseNo().equals(houseCode)){		//房屋编号修改时
				Room r = service.findByHouseCode(houseCode);
				if(r != null){
					rrequest.getWResponse().getMessageCollector().warn("此房屋编号已存在！", false, Consts.STATECODE_FAILED);
				}
			}
			
			if(!room.getBuilding().getId().equals(bid) || !room.getUnit().equals(unit) || !room.getRoomNo().equals(roomNo)){	//地址信息没有修改
				list = service.findByBuildingAndUnitAndRoomNo(bid, unit, roomNo);
				if(list != null && !list.isEmpty()){
					rrequest.getWResponse().getMessageCollector().warn("此室号已存在！", false, Consts.STATECODE_FAILED);
				}
			}
			
		}
	}

	
}

